/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

/**
 *
 * @author Administrator
 */
public class MenuCanvas extends GameCanvas implements Runnable {

    //midlet
    private MobileMonstersMIDlet midlet;
    private Display display;
    //path
    //private String pathShadow = "";
    //private String pathTitle = "";
    //path level
    //images        
    //private Image imgTitle;
    //button
    //font
    public static Font SMALL_PLAIN_FONT;
    public static Font SMALL_BOLD_FONT;
    public static Font LARGE_BOLD_FONT;
    //canvas property
    //private int CANVAS_WIDTH = getWidth();
    //private int CANVAS_HEIGHT = getHeight();
    //private int bgWidth = 0;
    //private int warna;
    //positions
    private int x = 0;
    private int y = 0;
    //private int xShadow = 0;
    //private int yShadow = 0;
    private int xPuato = 0;
    private int yPuato = 0;
    //private int jarakLevel = 0;
    private int xyArea[] = new int[4];
    //animated sprite
    //private AnimatedSprite spShadow;
    private Sprite spPuato;
    //private AnimatedSprite spBlueBird;
    private static int FRAME_SHADOW_WIDTH = 0;
    private static int FRAME_SHADOW_HEIGHT = 0;
    private static int FRAME_PUATO_WIDTH = 0;
    private static int FRAME_PUATO_HEIGHT = 0;
    private boolean running = false;
    private LayerManager lmgr;
    //speed 
    private int speedGame = 200;
    private int xMenu = 0;
    private int xJarak = 0;
    private int kursorMenu = 0;
    String sMenu[] = new String[8];
    private int statusPet = 0;//
    int arahPet = 0; //kiri
    private int WIDTH = 320;
    private int HEIGHT = 240;
    private String statusKey = "";
    
    Player audioPlayer01;
    //Player audioPlayer02;
    private int waktuBOBODOan = 0;

    public MenuCanvas(MobileMonstersMIDlet midlet) {
        super(false);
        setFullScreenMode(true);

        this.midlet = midlet;

        xMenu = (int) (getHeight() * 0.07);

        //jarakLevel = (int) (CANVAS_WIDTH * 0.9);

        sMenu[1] = "New";
        sMenu[2] = "Continue";
        sMenu[3] = "Setting";
        sMenu[4] = "Help";
        sMenu[5] = "Tutorial";
        sMenu[6] = "About";
        sMenu[7] = "Quit";

        kursorMenu = 1;

        FRAME_PUATO_WIDTH = 50;
        FRAME_PUATO_HEIGHT = 50;
        FRAME_SHADOW_WIDTH = 61;
        FRAME_SHADOW_HEIGHT = 75;

        //bgWidth = 640;
        SMALL_PLAIN_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        SMALL_BOLD_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        LARGE_BOLD_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);

        try {
            // nonanimated sprites
            //spStar = new StarSprite(Image.createImage("/star.png"), 10, 10);

            //animated sprite
            spPuato = new Sprite(Image.createImage("/puato.png"),
                    FRAME_PUATO_WIDTH, FRAME_PUATO_HEIGHT);
            xPuato = 10;
            yPuato = 10;
            spPuato.setPosition(xPuato, yPuato);

            //change the reference pixel to the middle of sprite
            //spPuato.defineReferencePixel(FRAME_WIDTH / 2, FRAME_HEIGHT / 2);

            //center the sprite on the canvas
            //(center of sprite is now in center of display)
            //spSpiral.setRefPixelPosition(getWidth() / 2, getHeight() / 2);

            // create and add to layer manager
            lmgr = new LayerManager();
            lmgr.append(spPuato);
            //lmgr.append(spStar);
        } catch (Exception e) {
        }

        if (MobileMonstersMIDlet.LANDSCAPE == 1) {
            xJarak = 26;
        } else {
            xJarak = 30;
            setPortraitDevice();
        }


        try {
            InputStream in = getClass().getResourceAsStream("/audio/menu.mid");
            audioPlayer01 = Manager.createPlayer(in, "audio/midi");
            //audioPlayer01.setLoopCount(0);
            //audioPlayer01.prefetch();
            //audioPlayer01.realize();
            audioPlayer01.start();

            //audioPlayer02 = Manager.createPlayer(in, "audio/midi");
            //audioPlayer02.setLoopCount(0);
            //audioPlayer02.prefetch();
            //audioPlayer02.realize();

        } catch (IOException ex) {
        } catch (MediaException ex) {
        }
        //
        //InputStream in = getClass().getResourceAsStream("/cobi.amr");
        //Player audioPlayer01 = Manager.createPlayer(in, "audio/amr");
        //Player p = Manager.createPlayer("capture://audio?audio_enc=amr");
        //Player p2 = Manager.createPlayer(recordedInputStream,"audio/amr");
        //audioPlayer01.start();
        //audioPlayer01.setLoopCount(-1);

    }

    //start thread
    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    //game loop
    public void run() {
        Graphics g = getGraphics();
        while (running) {
            // look for keypress
            checkForKeys();
            //checkForCollision();
            drawDisplay(g);

            waktuBOBODOan++;
            System.out.println(waktuBOBODOan);
            if(waktuBOBODOan==32){
                try {
                    audioPlayer01.stop();
                    
                } catch (MediaException ex) {
                }
                /*
                try {
                    //audioPlayer02.start();
                } catch (MediaException ex) {
                }
                 * 
                 */

            }else if(waktuBOBODOan==64){
                waktuBOBODOan=0;
                try {
                    audioPlayer01.start();
                    //audioPlayer02.stop();
                } catch (MediaException ex) {
                }
            }
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }
    }

    public void stop() {
        running = false;
    }

    protected void drawDisplay(Graphics g) {
        g.setColor(0x00ff00);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(0x0000ff);
        g.drawString(sMenu[1], 50, xMenu + xJarak * 1, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[2], 50, xMenu + xJarak * 2, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[3], 50, xMenu + xJarak * 3, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[4], 50, xMenu + xJarak * 4, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[5], 50, xMenu + xJarak * 5, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[6], 50, xMenu + xJarak * 6, Graphics.TOP | Graphics.LEFT);
        g.drawString(sMenu[7], 50, xMenu + xJarak * 7, Graphics.TOP | Graphics.LEFT);

        drawHighLight(g, kursorMenu);
        //g.drawString("for test only : " + getWidth() + "x" + getHeight(), 0, 10, Graphics.TOP | Graphics.LEFT);

        //animated sprite, show next frame in sequence

        spPuato.nextFrame();

        if (statusPet == 1) {
            //setWalking();
            petWalking();
        }
        //System.out.println(yPuato);
        spPuato.setPosition(xPuato, yPuato);

        g.drawString(statusKey, 30, 10, Graphics.TOP | Graphics.LEFT);

        //paint all layers
        lmgr.paint(g, 0, 0);
        flushGraphics();
    }

    private void setPortraitDevice() {
        xPuato = HEIGHT - 50;
        yPuato = 50;
        spPuato.setTransform(Sprite.TRANS_ROT90);
    }

    private void setEating() {
        int[] seq = new int[2];
        seq[0] = 3;
        seq[1] = 4;
        spPuato.setFrameSequence(seq);
    }

    private void setWalking() {
        int[] seq = new int[3];
        seq[0] = 11;
        seq[1] = 10;
        seq[2] = 9;
        spPuato.setFrameSequence(seq);
        statusPet = 1;
    }

    private void petWalking() {

        if (MobileMonstersMIDlet.LANDSCAPE == 1) {
            if (arahPet == 1) { //kiri
                if (xPuato > 10) {
                    xPuato = xPuato - 5;
                } else {
                    arahPet = 2;
                }
            } else { //kanan
                spPuato.setTransform(Sprite.TRANS_MIRROR);
                if (xPuato < 100) {
                    xPuato = xPuato + 5;
                } else {
                    spPuato.setTransform(Sprite.TRANS_NONE);
                    arahPet = 1;
                }
            }
        } else {
            //System.out.println("portraite");
            if (arahPet == 1) { //atas-KIRI
                if (yPuato > 10) {
                    yPuato = yPuato - 5;

                } else {
                    arahPet = 2;
                }
            } else { //bawah-KANAN                
                spPuato.setTransform(Sprite.TRANS_MIRROR_ROT90);
                if (yPuato < 100) {
                    yPuato = yPuato + 5;
                    //System.out.println("cek cuy");
                } else {
                    //spPuato.setTransform(Sprite.TRANS_NONE);
                    //System.out.println("arag - 1");
                    spPuato.setTransform(Sprite.TRANS_ROT90);
                    arahPet = 1;
                }
            }
        }

    }

    private void drawHighLight(Graphics g, int i) {
        g.setColor(0xff0000);
        g.drawString(sMenu[i], 50, xMenu + xJarak * i, Graphics.TOP | Graphics.LEFT);
    }

    private void checkForKeys() {
        int keyState = getKeyStates();

        if ((keyState & FIRE_PRESSED) != 0) {
            //isAttack = true;
            //keyPressed(-5);
            //System.out.println(getKeyStates());
        } else if ((keyState & LEFT_PRESSED) != 0) {
            //keyPressed(-3);
            //System.out.println(getKeyStates());
        } else if ((keyState & RIGHT_PRESSED) != 0) {
            //keyPressed(-4);
            //System.out.println(getKeyStates());
        } else if ((keyState & UP_PRESSED) != 0) {
            //keyPressed(-1);
            //System.out.println(getKeyStates());
        } else if ((keyState & DOWN_PRESSED) != 0) {
            //keyPressed(-2);
            //System.out.println(getKeyStates());
        } else if ((keyState & GAME_A_PRESSED) != 0) {
            //System.out.println("A!!!");
        } else if ((keyState & GAME_B_PRESSED) != 0) {
            //System.out.println("B!!!");
        } else if ((keyState & GAME_C_PRESSED) != 0) {
            //System.out.println("C!!!");
        } else if ((keyState & GAME_D_PRESSED) != 0) {
            //System.out.println("D!!!");
        }
    }

    protected void keyPressed(int keyCode) {

        statusKey = String.valueOf(keyCode);
        if (MobileMonstersMIDlet.LANDSCAPE == 1) { //landscape
            if (keyCode == -5) {
                if (kursorMenu == 7) {
                    midlet.exit();
                }
            } else if (keyCode == -2) { //down
                if (kursorMenu == 7) { //down
                    kursorMenu = 0;
                }
                kursorMenu++;
                setEating();

            } else if (keyCode == -4) { //right
            } else if (keyCode == -1) { //up
                if (kursorMenu == 1) { //up
                    kursorMenu = 8;
                }
                kursorMenu--;
                setWalking();
            } else if (keyCode == -3) { //left
            } else if (keyCode == -7) {
                midlet.exit();
            } else if (keyCode == -6) {
            } else if (keyCode == 50) { //LA-UP 2
            } else if (keyCode == 52) { //LA-LEFT 4
            } else if (keyCode == 54) { //LA-RIGHT 6
            } else if (keyCode == 56) { //LA-DOWN 8
            }

        } else {
            //portrait
            if (keyCode == -5) {
                if (kursorMenu == 7) {
                    midlet.exit();
                }
            } else if (keyCode == -2) { //down-RIGHT                
            } else if (keyCode == -4) { //right-UP
                if (kursorMenu == 1) { //up
                    kursorMenu = 8;
                }
                kursorMenu--;
                setWalking();
            } else if (keyCode == -1) { //up-LEFT
            } else if (keyCode == -3) { //left-DOWN
                if (kursorMenu == 7) {//down
                    kursorMenu = 0;
                }
                kursorMenu++;
                setEating();
            } else if (keyCode == -7) {
                midlet.exit();
            } else if (keyCode == -6) {
            } else if (keyCode == 50) { //LEFT 2
            } else if (keyCode == 52) { //DOWN 4
                if (kursorMenu == 7) {//down
                    kursorMenu = 0;
                }
                kursorMenu++;
                setEating();
            } else if (keyCode == 54) { //UP 6

                if (kursorMenu == 1) { //up
                    kursorMenu = 8;
                }
                kursorMenu--;
                setWalking();
            } else if (keyCode == 56) { //RIGHT 8
            }
        }

        repaint();
    }

    protected void pointerPressed(int x, int y) {
        //recognizer = new GestureRecognizer(x, y);
        this.x = x;
        this.y = y;
        //show = false;
    }

    protected void pointerReleased(int x, int y) {
        //recognizer.endGesture(x, y);
        this.x = x;
        this.y = y;
        //show = true;
    }
}
